<?php 
if (isset($set_modules) && $set_modules == TRUE)
{
    $i = isset($modules) ? count($modules) : 0;

    $modules[$i]['code']    = basename(__FILE__, '.class.php');
    $modules[$i]['name']    = L('online2', '', 'pay');   
    $modules[$i]['desc']    = L('online_tip', '', 'pay');
    $modules[$i]['is_cod']  = '0';
    $modules[$i]['is_online']  = '1';
    $modules[$i]['author']  = 'Old Sea';
    $modules[$i]['website'] = 'http://www.5aphp.com';
    $modules[$i]['version'] = '1.0.0';
    $modules[$i]['config']  = array(
     	array('name' => 'online_account','type' => 'text','value' => ''),
        array('name' => 'online_key','type' => 'text','value' => ''),
    );

    return;
}
pc_base::load_app_class('pay_abstract','','0');

class Online extends paymentabstract{
	
	public function __construct($config = array()) {	
		if (!empty($config)) $this->set_config($config);
      //测试环境 
	  
		//$this->config['gateway_url'] = 'http://58.246.226.99/UpopWeb/api/Pay.action';
		$this->config['gateway_url'] = 'https://unionpaysecure.com/api/Pay.action';
		$this->config['gateway_method'] = 'POST';
		$this->config['return_url'] = return_url('online');
		
		
		//pc_base::load_app_func('online');
	}
	public function getpreparedata2()
	{
				require_once('quickpay_service.php');
		$key = $this->config['online_key'];

		$param['transType']             = quickpay_conf::PRE_AUTH;  //交易类型，CONSUME or PRE_AUTH
		$param['orderAmount']           = $this->product_info['price']*100;        //交易金额
		$param['orderNumber']           = $this->order_info['id']; //订单号，必须唯一
		$param['orderTime']             = date('YmdHis');   //交易时间, YYYYmmhhddHHMMSS
		$param['orderCurrency']         = quickpay_conf::CURRENCY_CNY;  //交易币种，CURRENCY_CNY=>人民币
		$param['customerIp']            = ip();  //用户IP
		$param['frontEndUrl']           = $this->config['return_url'];    //前台回调URL
		$param['backEndUrl']            = $this->config['return_url'];    //后台回调URL
		
		$pay_service = new quickpay_service($param, quickpay_conf::FRONT_PAY);
		$prepare_data = $pay_service->args;
		
		return $prepare_data;
	}
	public function getpreparedata() {	
		return $this->getpreparedata2();
	}
	
	/**
	 * 客户端接收数据
	 * 状态码说明  （0 交易完成 1 交易失败 2 交易超时 3 交易处理中 4 交易未支付）
	 */
    public function receive() {
		require_once('quickpay_service.php');
		$response = new quickpay_service($_POST, quickpay_conf::RESPONSE);
		
		if ($response->get('respCode') == quickpay_service::RESP_SUCCESS) 
		{
       		$arr_ret = $response->get_args();
			$return_data['order_id'] = $arr_ret['orderNumber'];
			$return_data['order_status'] = 0;	
			return $return_data;
    	}
   		else
		{
			error_log(date('m-d H:i:s',SYS_TIME).'| GET: no return |'."\r\n", 3, CACHE_PATH.'pay_error_log.php');
			showmessage(L('illegal_return'));
			return false;
		}  
    }	

    /**
	 * POST接收数据
	 * 状态码说明  （0 交易完成 1 交易失败 2 交易超时 3 交易处理中 4 交易未支付）
	 */
    public function notify() {
    	require_once('quickpay_service.php');
		$response = new quickpay_service($_POST, quickpay_conf::RESPONSE);
		
		if ($response->get('respCode') == quickpay_service::RESP_SUCCESS) 
		{
       		$arr_ret = $response->get_args();
			$return_data['order_id'] = $arr_ret['orderNumber'];
			$return_data['order_status'] = 0;	
			return $return_data;
    	}
   		else
		{
			error_log(date('m-d H:i:s',SYS_TIME).'| GET: no return |'."\r\n", 3, CACHE_PATH.'pay_error_log.php');
			showmessage(L('illegal_return'));
			return false;
		}  
    }
    	
    /**
     * 相应服务器应答状态
     * @param $result
     */
    public function response($result) {
    	if (FALSE == $result) echo 'ok';
		else echo 'success';
    }
    
    /**
     * 返回字符过滤
     * @param $parameter
     */
	private function filterParameter($parameter)
	{
		$para = array();
		foreach ($parameter as $key => $value)
		{
			if ('sign' == $key || 'sign_type' == $key || '' == $value || 'm' == $key  || 'a' == $key  || 'c' == $key   || 'code' == $key ) continue;
			else $para[$key] = $value;
		}
		return $para;
	}
}
?>